﻿using System.Collections.Generic;
using ApplDbConvertors;
using Entities;
using ProductFileDbAccessLayer;
using ProductFileGlobalDefs;
using ProductFileObjectModel;

namespace ApplLogic
{
    public class LoginHandlerManager
    {
        private List<OMUser> m_LoggedInUsers;

        public LoginHandlerManager()
        {
            m_LoggedInUsers = new List<OMUser>();
        }

        public eGeneralRetCode Login(string userName, string password, ref eUserType typeToFill)
        {
            eGeneralRetCode returnCode = eGeneralRetCode.Error;
            OMUser userRef = verifyLogin(userName, password, ref typeToFill);
            if(userRef != null)
            {
                returnCode = eGeneralRetCode.Ok;
                m_LoggedInUsers.Add(userRef);
            }

            return returnCode;
        }

        private OMUser verifyLogin(string userName, string password, ref eUserType typeToFill)
        {
            OMUser applUser = null;
            User dbuser = DbApplContext.Instance.GetUser(userName, password);
            if (dbuser != null)
            {
                applUser = DbToApplConvertor.ConvertDbUserToApplUser(dbuser);
                typeToFill = applUser.UserType;
            }
            return applUser;
        }

    }
}
